package cn.lunkr.webview_sync_cookies.util;

import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;

import java.net.MalformedURLException;
import java.net.URL;


public class SystemUtil {

    private static final String TAG = "SystemUtil";


    /**
     * 打印cursor的内容<br/>
     * <b>这是测试方法，正式版本请勿使用</b>
     *
     * @param cursor
     * @return
     */
    public static String getCursorString(Cursor cursor) {
        int position = cursor.getPosition();
        StringBuffer buffer = new StringBuffer();
        cursor.moveToPrevious();
        while (cursor.moveToNext()) {
            for (String string : cursor.getColumnNames()) {
                buffer.append(string).append("=").append(cursor.getString(cursor.getColumnIndex(string))).append(",");
            }
            buffer.append("\n");
        }
        cursor.moveToPosition(position);
        return buffer.toString();
    }

    public static int getWidth(Context context) {
       /* if (context instanceof Activity) {
            DisplayMetrics displaysMetrics = new DisplayMetrics();
            ((Activity) context).getWindowManager()
                    .getDefaultDisplay()
                    .getMetrics(displaysMetrics);

            //得到屏幕宽
            return displaysMetrics.widthPixels;
        }
        return 0;*/
        return context.getResources().getDisplayMetrics().widthPixels;
    }


//    public static boolean isNewThanLocal(String localVersion, String remoteVersion) {
//        if (TextUtils.isEmpty(remoteVersion)) {
//            return false;
//        }
//        if (TextUtils.isEmpty(localVersion)) {
//            return false;
//        }
//        try {
//            String[] remotes = remoteVersion.split("\\.");
//            String[] locals = localVersion.split("\\.");
//            //本地是4位，服务器是3到4位。如果这个循环还解决不了，应该是本地多了一位.x,应该是本地更新
//            int length = Math.min(remotes.length, locals.length);
//            for (int i = 0; i < length; i++) {
//                int r = Integer.parseInt(remotes[i]);
//                int l = Integer.parseInt(locals[i]);
//                if (r > l) {
//                    return true;
//                }
//            }
//            return false;
//        } catch (Exception e) {  //不要为了可有可无的功能导致崩溃
//            LogUtil.e(TAG, "compare version failed", e);
//            return false;
//        }
//    }

//    public static boolean isNetworkAndSocketConnected() {
//        return NetConfig.isAvailable() && CApp.get().mSocketUsable.get();
//    }

    public static String getUrlDomain(String link) {
        String domain = "";
        if (!TextUtils.isEmpty(link)) {
            try {
                URL url = new URL(link);
                domain = url.getProtocol() + "://" + url.getHost();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        return domain;
    }

    /**
     * 判断是否是http链接
     *
     * @param link
     * @return
     */
    public static boolean isLink(String link) {
        return !TextUtils.isEmpty(link) && (link.startsWith("http://") || link.startsWith("https://"));
    }
}
